﻿using WRJClient.Model;
using WRJClient.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WRJClient.ViewModel;

namespace WRJClient.Net
{
    public class InformationMessageProcessor : IMessageProcessor
    {
        private bool parseInfomation(string jsonData, out InfomationModel infomationModel)
        {
            infomationModel = null;
            try
            {
                var infomationData = JsonConvert.DeserializeObject<dynamic>(jsonData);
                infomationModel = new InfomationModel
                {
                    DbId = infomationData.dbId,
                    Name = infomationData.name,
                    MissionId = infomationData.missionId,
                    Time = infomationData.time,
                    Type = infomationData.type,
                    Priority = infomationData.priority,
                    Desc = infomationData.desc,
                    Position = infomationData.position
                };
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }
            return true;
        }
        public void Process(TopicData topicData)
        {
            switch (topicData.eventId)
            {
                case 1://情报新建事件
                    InfomationAddProcess(topicData);
                    break;
                case 2://情报变更事件
                    InfomationUpdateProcess(topicData);
                    break;
                case 3://情报删除事件
                    InfomationDelProcess(topicData);
                    break;
                default:
                    MessageManager.Instance.Error(string.Format("未知情报主题[{0}]", topicData.eventId));
                    break;
            }
        }

        private void InfomationAddProcess(TopicData topicData)
        {
            InfomationModel model = null;
            if (parseInfomation(topicData.data, out model))
            {
                InfomationManager.Instance.Add(new InfomationViewModel(model));
                UpdateManager.Instance.Count();
                MessageManager.Instance.Info(string.Format("新建情报[{0}]!", model.Name));
            }
            else
            {
                MessageManager.Instance.Error(string.Format("新建情报[{0}]!", topicData.id), topicData.data);
            }
        }
        private void InfomationUpdateProcess(TopicData topicData)
        {
            InfomationModel model = null;
            if (parseInfomation(topicData.data, out model))
            {
                UpdateManager.Instance.Count();
                MessageManager.Instance.Info(string.Format("更新情报[{0}]!", model.Name));
            }
            else
            {
                MessageManager.Instance.Error(string.Format("更新情报[{0}]!", topicData.id), topicData.data);
            }
        }
        private void InfomationDelProcess(TopicData topicData)
        {
            InfomationModel model = null;
            if (parseInfomation(topicData.data, out model))
            {
                InfomationManager.Instance.Remove(new InfomationViewModel(model));
                UpdateManager.Instance.Count();
                MessageManager.Instance.Info(string.Format("删除情报[{0}]!", model.Name));
            }
            else
            {
                MessageManager.Instance.Error(string.Format("删除情报[{0}]!", topicData.id), topicData.data);
            }
        }
    }
}
